package com.huitone.gddw.controller.worktable.paramconfigmanage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.PriAlloc;
import com.huitone.gddw.dao.entity.PriRuleDef;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IPriAllocService;
import com.huitone.gddw.service.IPriRuleDefService;
import com.huitone.gddw.service.dto.PriRuleDefDTO;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 优先级规则 控制器
 * @author wuluodan
 *
 */
@Controller
@RequestMapping(value="priRuleDef")
public class PriRuleDefController extends CommonController {

	private final static Logger LOGGER = Logger.getLogger(PriRuleDefController.class);
	
	@Autowired
	private IPriRuleDefService priRuleDefService;
	
	@Autowired
	private IPriAllocService priAllocService;
	
	/**
	 * 查询
	 * @param page
	 * @param request
	 * @return QueryPage
	 */
	@ResponseBody
	@RequestMapping(value="query")
	public QueryPage<PriRuleDefDTO> query(QueryPage<PriRuleDef> page, HttpServletRequest request) {
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]查询优先级规则";
		Map<String, Object> params = new HashMap<String, Object>();
		String ruleId = request.getParameter("ruleId");
		params.put("ruleId", ruleId);
		page.setParams(params);
		QueryPage<PriRuleDefDTO> result = null;
		try {
			result = priRuleDefService.selectPage(page);
		} catch (Exception e) {
			LOGGER.error("查询附加条件规则信息出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return result;
	}
	
	/**
	 * 获取所有的优先级规则
	 * @return List
	 */
	@ResponseBody
	@RequestMapping(value="getAll")
	public List<PriRuleDef> getAll() {
		try {
			List<PriRuleDef> list = priRuleDefService.getAll();
			return list;
		} catch (Exception e) {
			LOGGER.error("获取所有的短信业务信息过程中出现异常：" + e.getMessage(), e);
			return new ArrayList<PriRuleDef>();
		}
	}
	
	/**
	 * 更改状态
	 * @param ruleId 规则ID
	 * @param status 更改后状态
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="changeStatus")
	public Message changeStatus(String ruleId, String status) {
		try {
			PriRuleDef entity = priRuleDefService.selectByPrimaryKey(ruleId);
			entity.setStatus(status);
			priRuleDefService.changeStatus(entity);
			return new Message(true, "操作成功！");
		} catch (Exception e) {
			LOGGER.error("更改优先级规则状态过程中出现异常：" + e.getMessage(), e);
			return new Message(false, "操作失败！");
		}
	}
	
	/**
	 * 修改页面数据准备
	 * @param ruleId 规则ID
	 * @return PriRuleDefDTO
	 */
	@ResponseBody
	@RequestMapping(value="toUpdate")
	public PriRuleDefDTO toUpdate(String ruleId) {
		try {
			PriRuleDefDTO entity = priRuleDefService.toUpdate(ruleId);
			return entity;
		} catch (Exception e) {
			LOGGER.error("跳转修改页面过程中出现异常：" + e.getMessage(), e);
			return new PriRuleDefDTO();
		}
	}
	
	/**
	 * 保存
	 * @param entity PriRuleDef
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="save")
	public Message save(PriRuleDef entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]添加优先级规则";
		boolean isSuccess = true;
		String msg;
		try {
			List<PriRuleDef> list = priRuleDefService.getByLevel(entity.getLevel());
			if (!list.isEmpty() && list.size() > 0) {
				isSuccess = false;
				msg = "该优先级已存在，请重新输入！";
			} else {
				priRuleDefService.save(entity);
				msg = "保存成功！";
			}
		} catch (Exception e) {
			LOGGER.error("保存优先级规则信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "保存失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	/**
	 * 更新
	 * @param entity PriRuleDef
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="update")
	public Message update(PriRuleDef entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]修改优先级规则";
		boolean isSuccess = true;
		String msg;
		try {
			PriRuleDef priRuleDef = priRuleDefService.selectByPrimaryKey(entity.getRuleId());
			if (!entity.getLevel().equals(priRuleDef.getLevel())) {
				List<PriRuleDef> list = priRuleDefService.getByLevel(entity.getLevel());
				if (!list.isEmpty() && list.size() > 0) {
					isSuccess = false;
					msg = "该优先级已存在，请重新输入！";
					super.logResult = SmLog.RESULT_FAIL;
					super.saveSmLog(super.logType, super.logContent, super.logResult);
					return new Message(isSuccess, msg);
				}
			}
			priRuleDef.setStatus(entity.getStatus());
			priRuleDef.setLevel(entity.getLevel());
			priRuleDef.setDesc(entity.getDesc());
			priRuleDefService.update(priRuleDef);
			msg = "修改成功！";
		} catch (Exception e) {
			LOGGER.error("修改优先级规则信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "修改失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	/**
	 * 删除
	 * @param ruleId 规则ID
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public Message delete(String ruleId) {
		super.logType = SmLog.LOGTYPE_DELETE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]删除优先级规则";
		try {
			List<PriAlloc> list = priAllocService.getByRuleId(ruleId);
			if (!list.isEmpty() || list.size() > 0) {
				super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
				return new Message(false, "该优先级已被其它地方引用，无法删除！");
			}
			priRuleDefService.delete(ruleId);
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(true, "删除成功！");
		} catch (Exception e) {
			LOGGER.error("删除优先级规则信息过程中出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return new Message(false, "删除失败！");
		}
	}
	
}
